package com.hoshiibuy.company.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.company.feign.INeo4jService;
import com.hoshiibuy.company.mapper.ICompanyMapper;
import com.hoshiibuy.company.feign.IdGeneratorService;
import com.hoshiibuy.company.entity.Company;
import com.hoshiibuy.company.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 公司 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<ICompanyMapper, Company> implements ICompanyService {



    @Autowired
    private IdGeneratorService idGeneratorService;

    @Autowired
    INeo4jService neo4jService;

    /**
     * @param id
     * @return com.hoshiibuy.company.vo.company
     * @description: 根据id查询平台信息
     */
    @Cacheable(cacheNames = "company", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public Company getById(Long id) {
        return baseMapper.selectById(id);
    }



    /**
     * @param
     * @return boolean
     * @description: 新增入住公司
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "company", allEntries = true)

    @Override
    public boolean save(Company entity) {

        String id = idGeneratorService.queryGeneratorId();

        if (id == null) {
            throw new ServiceException(CommonError.error("新增入住公司失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增入住公司失败"));
        }
        /**
         * 先写入neo4j数据库
         */
        JSONObject json = generaNeo4jJson(entity, id);
        //System.out.println(json.toJSONString());
        Response tenant = neo4jService.createTenant(json.toString());
        if(200 !=tenant.getCode()){
            throw new ServiceException(CommonError.error("同步租户到neo4j数据库失败"));
        }
        return true;
    }

    private JSONObject generaNeo4jJson(Company entity, String id) {
        JSONObject json = new JSONObject();
        json.put("label",entity.getName());
        Map<String,String> properties = new HashMap<String,String>();
        properties.put("name",entity.getName());
        properties.put("creditcode",entity.getCreditcode());
        properties.put("idcard",entity.getIdCard());
        Object toJSON = JSONObject.toJSON(properties);
        json.put("properties",toJSON);
        json.put("uuid",id);
        return json;
    }

    /**
     * @param company
     * @return boolean
     * @description: 修改入住公司信息
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "company", allEntries = true)
    @Override
    public boolean updateById(Company company) {

        company.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.updateById(company);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改入住公司失败"));
        }
        return true;
    }
}
